package fr.afcepf.al22.qualite.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.sql.DataSource;




import fr.afcepf.al22.qualite.data.util.Al22DataSource;
import fr.afcepf.al22.qualite.entitee.Personne;

public class DaoPersonneImpl implements IDaoPersonne {
    private DataSource ds = new Al22DataSource();

    private String reqAjout =
            "INSERT INTO personne(nom,mail,mdp,naissance)"
            + "VALUES (?,?,?,?)";
    /**
     * indice de parametre nom dans la requete.
     */
    private final int indiceNomAjout = 1;
    /**
     * Indice de parametre Mail dans la requete.
     */
    private final int indiceMailAjout = 2;
    /**
     * Indice de parametre MDP.
     */
    private final int indiceMdpAjout = 3;
    private final int indiceNaissanceAjout = 4;
    private final int indiceIdGenere = 0;
    @Override
    public Personne ajouter(Personne paramPers) {
          Connection cnx = null;
          try {
              cnx = ds.getConnection();
              PreparedStatement pstmt =
                      cnx.prepareStatement("", Statement.RETURN_GENERATED_KEYS);
              pstmt.setString(indiceMdpAjout, paramPers.getMdp());
              pstmt.setString(indiceNomAjout, paramPers.getNom());

              pstmt.setString(indiceMailAjout, paramPers.getMail());
              pstmt.setDate(indiceNaissanceAjout,
                      new java.sql.Date(paramPers.getNaissance().getTime()));
              int nbLigne = pstmt.executeUpdate();
              ResultSet rsKey = pstmt.getGeneratedKeys();
              if (nbLigne == 1 && rsKey.next()) {
                  paramPers.setId(rsKey.getInt(indiceIdGenere));
              }
          } catch (SQLException e) {
              e.printStackTrace();
      } finally {
          try {
              cnx.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }

        return paramPers;
    }

    @Override
    public List<Personne> rechercher(String paramNom) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public Personne verifMail(String paramMail) {
        // TODO Auto-generated method stub
        return null;
    }

    public String getReqAjout() {
        return reqAjout;
    }

    public void setReqAjout(String paramReqAjout) {
        reqAjout = paramReqAjout;
    }




}
